/**
 * 安卓白金英雄坛制作组 <br>
 * 文件名：WaitingForJoinScreen.java <br>
 * 创建时间：2015年4月30日 下午9:15:01 <br>
 * 所属项目：GmudEX <br>
 * @author 12548
 */
package lostland.gmud.exv2.battle.netbattle;

import com.google.gson.Gson;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;

import org.json.JSONException;
import org.json.JSONObject;

import lostland.general.network.GmudHttp;
import lostland.general.network.GmudRequestListener;
import lostland.general.network.GmudRestUrl;
import lostland.gmud.exv2.FontSize;
import lostland.gmud.exv2.GameConstants;
import lostland.gmud.exv2.Gmud;
import lostland.gmud.exv2.battle.BattleScreen;
import lostland.gmud.exv2.battle.proc.holder.NetBattle;
import lostland.gmud.exv2.blgframework.impl.BLGGraphics;
import lostland.gmud.exv2.data.Npc;
import lostland.gmud.exv2.data.Room;
import lostland.gmud.exv2.data.SaveData;
import lostland.gmud.exv2.ui.NotificationScreen;
import lostland.gmud.exv2.ui.core.SlowScreen;

/**
 * 类名：WaitingForJoinScreen <br>
 * 说明：
 *
 * @author 12548
 */
public class WaitingForJoinScreen extends SlowScreen {

	String text = "正在等待对手加入......";


	private volatile boolean querying = false;

	public WaitingForJoinScreen() {
		super(2.0f);
	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.ui.core.SlowScreen#update(float)
	 */
	@Override
	public void update(float deltaTime) {
		if (this != getGame().getCurrentScreen())
			return;
		if (querying)
			return;
		super.update(deltaTime);
	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.ui.core.SlowScreen#onClick(int, int)
	 */
	@Override
	protected void onClick(int tx, int ty) {

		if (querying)
			return;

		//TODO
	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.ui.core.SlowScreen#tick()
	 */
	@Override
	public void tick() {
		__tickTime += 0.5f;
		query();
	}

	public void query() {

		querying = true;

		RequestParams rp = new RequestParams();

		rp.addBodyParameter("arena", BattleScreen.getInstance().battleInfo.getObjectId());
		rp.addBodyParameter("uid", BattleScreen.getInstance().uuid);
		rp.addBodyParameter("idstamp", BattleScreen.getInstance().idstamp + "");

		new GmudHttp().postBackground(GmudRestUrl.WaitForJoin, rp, new GmudRequestListener() {

			@Override
			public void onFail(HttpException error, String msg) {
				getGame().popToRootScreen();
				new NotificationScreen("网络错误！").pushToGame();
			}

			@Override
			public void onSucc(ResponseInfo<String> info) {
				JSONObject jo;
				try {
					jo = new JSONObject(info.result);
					String s = jo.getString(GmudHttp.KEY_STATE);

					if (s.equals(GmudHttp.STATE_ERROR)) {
						getGame().popToRootScreen();
						new NotificationScreen("服务器错误！！").pushToGame();
					} else if (s.equals(GmudHttp.STATE_OK)) {


						Gson gson = new Gson();
						SaveData sd = gson.fromJson(jo.getJSONObject("clientsv").toString(), SaveData.class);

						final Npc npc = new Room().npc(sd.mc);
						npc.refresh();
						int x = npc.look(-1, -1).id;
						getGame().popToRootScreen();
						BattleScreen.battle(-1, x, new NetBattle());
						BattleScreen.getInstance().networkMode = NetworkMode.HOST;

					} else if (s.equals(GmudHttp.STATE_EMPTY)) {
						querying = false;
					} else {
						getGame().popToRootScreen();
						new NotificationScreen("发生了未知错误！！").pushToGame();
					}
				} catch (JSONException e) {
					if (Gmud.DEBUG) {
						e.printStackTrace();
					}

					getGame().popToRootScreen();
					new NotificationScreen("发生了未知错误！！").pushToGame();
				}
			}
		});
	}


	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.blgframework.CScreen#present(float)
	 */
	@Override
	public void present(float deltaTime) {
		getParentScreen().present(0);
		BLGGraphics g = (BLGGraphics) getGame().getGraphics();
		g.drawRect(GameConstants.FBWIDTH / 2 - 80, GameConstants.FBHEIGHT / 2 - 25, 160, 50,
				Gmud.getBgColor());
		g.drawText(text, GameConstants.FBWIDTH / 2 - 80, GameConstants.FBHEIGHT / 2 - 25,
				FontSize.FT_12PX, 150);
	}


}
